package com.sjh.lib_app

import android.view.View
import android.view.ViewGroup
import com.google.android.material.appbar.AppBarLayout
import kotlin.math.abs

/**
 * 一级界面样式的滑动帮助类
 * 注：如果需要改变y轴的效果，那么zoomView的y必须跟随searchView，因为这里只改变了searchView的y值
 *
 * @param searchView 搜索框View
 * @param zoomView 左边需要缩放的View
 * @param scrollHeight 滑动距离，即最少滑动多少才能使sreachView的宽度达到最大
 * @param searchViewMaxWidth SreachView最大宽度
 * @param moveUpHight View上移距离，如果不需要上移，可以不传或者传0
 *
 * @author Extends
 * @date 2020/1/13/013
 */
class ScrollSearchHelper(
    private val searchView: View,
    private val zoomView: View,
    private val scrollHeight:Int,
    private val searchViewMaxWidth: Int,
    private val moveUpHight: Int = 0
) : AppBarLayout.OnOffsetChangedListener {
    private val density by lazy { searchView.resources.displayMetrics.density }
    //是否改变搜索框宽度
    private var isChangeWidth = false
    private var isChangeY = false

    //searchView的最小宽度
    private var searchViewWidth: Int = -1
    //zoomView的最大宽度
    private var zoomViewWidth: Int = -1
    //zoomView的最大高度
    private var zoomViewHeight: Int = -1
    private var zoomViewX : Float = Float.MIN_VALUE

    init {
        searchView.post {
            searchViewWidth = searchView.layoutParams.width
        }
        zoomView.post {
            zoomViewWidth = zoomView.layoutParams.width
            zoomViewHeight = zoomView.layoutParams.height

        }
    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        val absVerticalOffset = abs(verticalOffset)
        var offset = absVerticalOffset * 1f / scrollHeight
        if (offset >= 1f) {
            offset = 1f
        }
        //改变搜索框
        val lp = searchView.layoutParams as ViewGroup.MarginLayoutParams
        var newWidth = lp.width
        var newBottomMargin = lp.bottomMargin
        //改变宽度
        if (isChangeWidth) {
            newWidth = ((searchViewMaxWidth - searchViewWidth) * offset + searchViewWidth).toInt()
            //改变logo
            //如果zoomViewX没有赋值，则获取zoomView的起始x的值
            //在post里面没有获取到该值
            if(zoomViewX == Float.MIN_VALUE){
                zoomViewX = zoomView.x
            }
            zoomView.x = zoomViewX - zoomViewWidth * offset * 0.5f
            zoomView.scaleX = (1 - offset)
            zoomView.scaleY = (1 - offset)
        }
        isChangeWidth = offset > 0f && offset <= 1f
        //改变y轴
        if (moveUpHight != 0) {
            if (isChangeY && absVerticalOffset <= moveUpHight) {
                newBottomMargin = absVerticalOffset
            } else {
                //校验是否因为过快而导致没有移动高度
                if (absVerticalOffset == 0 && lp.bottomMargin != 0) {
                    newBottomMargin = 0
                } else if (absVerticalOffset > moveUpHight && lp.bottomMargin != moveUpHight) {
                    newBottomMargin = moveUpHight
                }
            }
            isChangeY = absVerticalOffset in 1..moveUpHight
        }
        if(newBottomMargin != lp.bottomMargin || newWidth != lp.width){
            lp.width = newWidth
            lp.bottomMargin = newBottomMargin
            searchView.layoutParams = lp
        }
//        searchView.layoutParams = lp
    }

    fun dip(dp: Int): Int = (density * dp).toInt()
     fun onOffsetChanged(verticalOffset: Int) {
        val absVerticalOffset = abs(verticalOffset)
        var offset = absVerticalOffset * 1f / scrollHeight
        if (offset >= 1f) {
            offset = 1f
        }
        //改变搜索框
        val lp = searchView.layoutParams as ViewGroup.MarginLayoutParams
        var newWidth = lp.width
        var newBottomMargin = lp.bottomMargin
        //改变宽度
        if (isChangeWidth) {
            newWidth = ((searchViewMaxWidth - searchViewWidth) * offset + searchViewWidth).toInt()
            //改变logo
            //如果zoomViewX没有赋值，则获取zoomView的起始x的值
            //在post里面没有获取到该值
            if(zoomViewX == Float.MIN_VALUE){
                zoomViewX = zoomView.x
            }
            zoomView.x = zoomViewX - zoomViewWidth * offset * 0.5f
            zoomView.scaleX = (1 - offset)
            zoomView.scaleY = (1 - offset)
        }
        isChangeWidth = offset > 0f && offset <= 1f
        //改变y轴
        if (moveUpHight != 0) {
            if (isChangeY && absVerticalOffset <= moveUpHight) {
                newBottomMargin = absVerticalOffset
            } else {
                //校验是否因为过快而导致没有移动高度
                if (absVerticalOffset == 0 && lp.bottomMargin != 0) {
                    newBottomMargin = 0
                } else if (absVerticalOffset > moveUpHight && lp.bottomMargin != moveUpHight) {
                    newBottomMargin = moveUpHight
                }
            }
            isChangeY = absVerticalOffset in 1..moveUpHight
        }
        if(newBottomMargin != lp.bottomMargin || newWidth != lp.width){
            lp.width = newWidth
            lp.bottomMargin = newBottomMargin
            searchView.layoutParams = lp
        }
//        searchView.layoutParams = lp
    }

}